import sys
import os
grandparent_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.append(grandparent_dir)

import time
from driving_route_planning import driving_route_planning
import pickle
from utils.tools import read_json

if __name__ == "__main__":
    # 配置信息
    config = read_json("path_planning/tianditu/config.jsonc")
    TK = config["TK"]
    origin_list = [
            '106.527506,29.625032',                         #两江新区第一人民医院
            '106.509320,29.532720',                         #重庆医科大学附属第一医院
            '106.470599,29.469566',                         #大渡口区人民医院
            '106.218850,29.592286',                         #重庆医科大学附属第一医院璧山医院(老院区)
            '106.309980,29.677840',                         #沙坪坝区中西医结合医院
            '106.227261,29.444497',                         #重庆医科大学附属第一医院璧山医院
            '106.361109,29.489657'                          #九龙坡区第五医院
    ]
    names = ["两江新区第一人民医院", 
            "重庆医科大学附属第一医院",
            "大渡口区人民医院",
            "重庆医科大学附属第一医院璧山医院(老院区)",
            "沙坪坝区中西医结合医院",
            "重庆医科大学附属第一医院璧山医院",
            "九龙坡区第五医院"]
    
    destination = '106.322350,29.607910'                    #富力院士廷
    destination_name = "富力院士廷"

    map_output_path = config["map_output_path"]
    data_output_path = config["data_output_path"]    
    for style in  [0,1]:                                    #默认0 （0：最快路线，1：最短路线，2：避开高速，3：步行）
        start = time.time()
        datas = []
        for origin in origin_list:
            data = driving_route_planning(TK, origin, destination, style)
            datas.append(data)
        end = time.time()

        print ("style", style)
        print ("路径规划总调用时间", end - start)
        print ("路径规划总调用次数", len(origin_list))

        coordinates_list = []
        for distance, duration, mapinfo, routelatlon in datas:
            coordinates = []
            for x in routelatlon[:-1].split(";"):
                coordinates.append([float(x.split(",")[1]), float(x.split(",")[0])])
            coordinates_list.append(coordinates)
        with open(data_output_path+"_style_%d.pkl" %(style), "wb") as haddle:
            pickle.dump((names, destination_name, origin_list, destination, coordinates_list, datas, style), haddle)

        